package com.me.ui;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.me.constant.Constant;
import com.me.helper.PreferencesData;
import com.me.memorytest.MainMemoryTest;
import com.me.screen.BaseScreen;
import com.me.utils.ScreenshotFactory;

public class HighScore extends BaseScreen{

	private Label lb_BestScore, lb_currentScore, lb_Score;
	private TextButton btn_shareGame;
	
	@Override
	public void show() {
		super.show();
		
		BaseScreen.stage.clear();
		
		HeaderGame.lb_Click.setText("");
		
		HeaderGame header = new HeaderGame("--KID MEMO QUIZ--", Constant.STAGE_WIDTH, Constant.STAGE_HEIGHT, 1);
		stage.addActor(header);
		
		lb_BestScore = new Label("Best click:   "
				+ PreferencesData.getHighScore(1), Constant.skin, "labelMenu");
		lb_BestScore.setX(280);
		lb_BestScore.setY(450);

		lb_currentScore = new Label("Your current click:  " + Constant.iCountClicked,
				Constant.skin, "labelMenuScore");
		lb_currentScore.setX(280);
		lb_currentScore.setY(350);

		lb_Score = new Label("Score", Constant.skin, "labelMenuScore");
		lb_Score.setX(1000);
		lb_Score.setY(550);

		btn_shareGame = new TextButton("Share score", Constant.skin,
				"textButton_45_bold");
		btn_shareGame.setX(440);
		btn_shareGame.setY(200);
		btn_shareGame.setWidth(450);
		btn_shareGame.setHeight(100);
		btn_shareGame.addListener(new ClickListener() {
			@Override
			public void clicked(InputEvent event, float x, float y) {
				super.clicked(event, x, y);
				Gdx.app.postRunnable(new Runnable() {
					@Override
					public void run() {
						if (!Constant.isSharedScore) {
							MainMemoryTest.getApi().showToast("Please wait!");
							if (!Gdx.files.getExternalStoragePath().isEmpty()) {
								ScreenshotFactory.saveScreenshot();
								String str = Gdx.files.getExternalStoragePath()
										+ "/score.png";
								MainMemoryTest.getApi().shareImage(str);
							} else {
								MainMemoryTest.getApi().showToast(
										"Sd card not found");
							}
							Constant.isSharedScore = true;
						} else {
							MainMemoryTest.getApi().showToast(
									"Back to menu, plz");
						}
					}
				});

			}

		});

		if(!Constant.b_HighScoreHome){
			if (Constant.iCountClicked < PreferencesData.getHighScore(1)) {
				PreferencesData.setHighScore(Constant.iCountClicked, 1);
			}
		}else{
			Constant.b_HighScoreHome = false;
		}

		stage.addActor(lb_currentScore);
		stage.addActor(lb_BestScore);
		stage.addActor(btn_shareGame);

		BaseScreen.stage.setViewport(Constant.STAGE_WIDTH, Constant.STAGE_HEIGHT, false);
	}
	
	@Override
	public void render(float arg0) {
		Gdx.gl.glClearColor(0, 0, 0.2f, 1);
		Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

		// tell the camera to update its matrices.
		camera.update();

		// tell the SpriteBatch to render in the coordinate system specified by the camera.
		sb.setProjectionMatrix(camera.combined);

		// begin a new batch and draw the bucket and all drops
		sb.begin();
		sb.end();
		stage.setViewport(Constant.STAGE_WIDTH, Constant.STAGE_HEIGHT,false);
	}
}
